# -*- coding: utf-8 -*-
 
import hashlib
from base import UserCase, Runner, TestSuite

class Case(UserCase):
    def create_corp(self):
        p = {
            'name': self.randhan(8),
            'username': self.randchar(12),
            'password': hashlib.md5(self.randchar(8)).hexdigest(),
        }
        res = self.c.open('/api/corp/create', p)
        return res['data']

    def create_device(self, corp=''):
        p = {
            'cpu_id': self.randchar(12),
            'name': self.randhan(8),
            'corp': corp,
        }
        res = self.c.open('/api/device/create', p)
        return res['data']

    def test_gen_data(self):
        corp = self.create_corp()['id']
        device = self.create_device(corp)['id']
        res = self.c.open('/api/device/gen-test-data', {'id': device})
        self.assertEqual(res['code'], 0)

    def test_data(self):
        corp = self.create_corp()['id']
        device = self.create_device(corp)['id']
        res = self.c.open('/api/device/gen-test-data', {'id': device})
        self.assertEqual(res['code'], 0)

        cpu_id = self.c.open('/api/device/read', {'id': device})['data']['cpu_id']
        res = self.c.open('/api/data', {'cpu_id': cpu_id, 'limit': 1})
        self.assertEqual(res['code'], 0)



test_list = [
    'test_gen_data',
    'test_data',
]
Suite = TestSuite([Case(t) for t in test_list])


if __name__ == '__main__':
    Runner.run(Suite)



